import datetime

from django.shortcuts import render
from django.views.generic.base import View

from utils.mixin_utils import LoginRequiredMixin
from login.models import UserProfile
from payroll.models import SentContents


# 首页-可以注释使用其他模块重写dasboard做首页
class IndexView(LoginRequiredMixin, View):
    def get(self, request):
        # 基础数据 Start
        # 获取用户总数，获取今日登录用户数
        totalUser = UserProfile.objects.count()
        today = datetime.datetime.now()
        today = today.strftime('%Y-%m-%d')
        today_has_login_user = UserProfile.objects.filter(last_login__contains=today).count()
        # 基础数据 End
        # 1，获取当月发送的工资条数量
        cur_year = datetime.datetime.now().year
        cur_month = datetime.datetime.now().month
        cur_month_send = SentContents.objects.filter(time__year=cur_year, time__month=cur_month).count()
        # 2，获取历史数量
        total_send = SentContents.objects.all().count()
        # 最新10条发送数据
        latest_10 = SentContents.objects.all()[:10]
        return render(request, 'index.html', {'totalUser': totalUser, 'today_has_login_user': today_has_login_user,
                                              'cur_month_send': cur_month_send, 'total_send': total_send,
                                              'cur_year': cur_year, 'cur_month': cur_month, 'latest_10': latest_10,
                                              })
